package com.wjv.example.controller;

import com.wjv.example.common.entity.ResultInfo;
import com.wjv.example.service.PaymentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>Title: PaymentController</p>
 * <p>Company:misspao </p>
 *
 * @author: wjv
 * @Date: Create in 15:20 2020/9/21
 * @Description:
 */
@RestController
@RequestMapping("/payment")
public class PaymentController {
    private static final Logger logger = LoggerFactory.getLogger(PaymentController.class);
    @Autowired
    private PaymentService paymentService;
    @Resource
    private DiscoveryClient discoveryClient;

    @GetMapping("/getPayment")
    public ResultInfo getPayment(int id) throws InterruptedException {
        logger.info("**************" + LocalDateTime.now().toString());
        if (id == 3) {
            Thread.sleep(3000);
        }
        return paymentService.getPayment(id);
    }

    @PostMapping("/save")
    public ResultInfo save() {
        return paymentService.save();
    }

    @GetMapping(value = "/discovery")
    public Object discovery(){
        List<String> services = discoveryClient.getServices();
        for (String element : services) {
            logger.info("***** element:"+element);
        }
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PROVIDER-PAYMENT");
        for (ServiceInstance instance : instances) {
            logger.info(instance.getServiceId()+"\t"+instance.getHost()+"\t"+instance.getPort()+"\t"+instance.getUri());
        }
        return this.discoveryClient;
    }

}
